/**
 * 功能描述 卡片
 */

define(function(require, exports, module) {
	var Form = require("form");
	var enmu = require("enums");
	var moment = require("moment");
	var applyFormEvents = require("../handler/applyFormEvents");
	var Dialog = require("dialog");
	var instance = {
		initComponent : function(params, widget) {
			var applyForm = new Form({
				parentNode : ".J-applyform",
				show : false,
				model : {
					id : "applyform",
					saveaction: function() {
						
						var travelContractObj = applyForm.getValue("travelContractObj");
						
						for(var i=0;i<$(".J-grid-columns-personalInfo-idNumber").length;i++){
							var text = $(".J-grid-columns-personalInfo-idNumber").eq(i).text();
							var number = $(":input[name='personalInfo.idNumber']").val();
							if(text==number){
								Dialog.alert({
				    				content:"身份证号不能重复"
				            	});
								return;
							}
						}
						
						if(!travelContractObj){
							Dialog.alert({
			    				content:"请输入会员证号"
			            	});
							return;
						}
						
                        var data = applyForm.getData();
                        applyFormEvents.saveAction(data, widget);
                    },
                    saveText: "保存",
                    cancelaction: function() {
                    	widget.show([".J-form", ".J-applygrid",".J-editgrid"]).hide(".J-applyform");
						widget.get("subnav").show(["cancel","save"]).hide(["travelDate", "destination", "servicePoint","applyCancel"]);
						widget.get("applyForm").reset();
                    },
                    cancelText: "取消",
					items : [{
                   	 	name : "pkSignUpRegister",
                   	 	type : "hidden",
					},{
                   	 	name : "version",
                   	 	type : "hidden",
					},{
						name : "personalInfo.pkPersonalInfo",
						type : "hidden",
					},{
						name : "personalInfo.version",
						type : "hidden",
					},{
						name : "pkPlanItem",
						type : "hidden",
					},{
						name : "pkServicePoint",
						type : "hidden",
					},{
						name : "member",
						label : "是否是会员",
						type : "radio",
						defaultValue : "false",
						list: [{
			                key: "true",
			                value: "是"
			            }, {
			                key: "false",
			                value: "否"
			            }],
			            validate: ["required"],
						events:{
							click:function(e){
								var value = e.target.value;
								if(value=="true"){
									applyForm.hide("personalInfo.name").show("memberObj");
									applyForm.setReadonly("personalInfo.idNumber",true);
									applyForm.setReadonly("personalInfo.mobilePhone",true);
									applyForm.setReadonly("personalInfo.sex",true);
								}else{
									applyForm.show("personalInfo.name").hide("memberObj");
									applyForm.setReadonly("personalInfo.idNumber",false);
									applyForm.setReadonly("personalInfo.mobilePhone",false);
									applyForm.setReadonly("personalInfo.sex",false);
								}
							}
						}
					},{
						name : "memberObj",
						label : "姓名",
						type:"autocomplete",
						url:"api/certificatestransfer/searchmember",
						keyField:"pkMember",
						valueField:"personalInfo.idNumber",
						queryParamName : "s",
						params:function(str){
							return{
								searchProperties:"personalInfo.name,personalInfo.idNumber", 
								fetchProperties:"pkMember,personalInfo.*"
							}
						},
						format:function(data){
							if(data.personalInfo){
								return data.personalInfo.name+"-"+data.personalInfo.idNumber;
							}else{
								return "";
							}
							
						},
						onItemSelect : function(data){
							applyForm.setValue("personalInfo.pkPersonalInfo",data.personalInfo.pkPersonalInfo);
							applyForm.setValue("personalInfo.version",data.personalInfo.version);
							//身份证号
							applyForm.setValue("personalInfo.idNumber",data.personalInfo.idNumber);
							//性别
							applyForm.setValue("personalInfo.sex",data.personalInfo.sex.key);	
							
							//联系方式
							if(data.personalInfo.mobilePhone){
								applyForm.setValue("personalInfo.mobilePhone",data.personalInfo.mobilePhone);
							};
						},
						validate : [ "required" ]
					},{
						name : "personalInfo.name",
						label : "姓名",
						type:"text",
						validate : [ "required" ]
					}, {
						name : "personalInfo.idNumber",
						label : "身份证号",
						type : "text",
						validate : [ "required" ]
					}, {
						name : "personalInfo.sex",
						label : "性别",
						type : "radio",
						defaultValue : "Male",
						list : enmu["com.eling.elcms.basedoc.model.Sex"],
						validate : [ "required" ]
					}, {
						name : "personalInfo.mobilePhone",
						type : "text",
						label : "手机号",
						validate : [ "required" ]
					}, {
						name : "travelContractObj",
						label : "会员证号",
						type: "autocomplete",
                        url: "api/signupregister/querycertificates",
                        params:function(str){
	                       	return {
	                       		"pkServicePoint":widget.get("subnav").getValue("servicePoint"),
	                       		"fetchProperties":"pkTravelContract,memberCertificates,version,member.personalInfo.name," +
	                       				"member.personalInfo.idNumber,productRule.product.name,productRule.travelNum,productRule.product.pkProduct," +
	                       				"productRule.product.version"
	                       	};
                        },
                        queryParamName: "s",
                        //代表所选的选项的值是哪一个字段的值
                        keyField: "pkTravelContract",
                        //代表选项下拉框中显示的值
                        format: function(data) {
                            return data.member.personalInfo.idNumber+"-"+data.member.personalInfo.name+
                            		"-"+data.productRule.product.name+"-"+data.memberCertificates;
                        },
                        onItemSelect : function(data){
                        	if(data.productRule.travelNum){
                        		var signupdatas=widget.get("applyGrid").getData()||[];
                        		var count =0;
                        		for ( var i in signupdatas) {
									if(signupdatas[i].travelContract.pkTravelContract ==data.pkTravelContract){
										count++;
									}
								}
                        		if(count >= Number(data.productRule.travelNum)){
                        			Dialog.alert({
        			    				content:"该会员证本次超过使用人数！"
        			            	});
                        			applyForm.getPlugin("travelContractObj").reset();
        							return false;
                        		}
                        	}
							applyForm.setValue("pkTravelContract",data.pkTravelContract);
						},
						validate : [ "required" ]
					}, {
						name : "pkTravelContract",
						type : "hidden",
					},{
						name : "signUpDate",
						type : "date",
						label : "报名日期",
						format:"YYYY-MM-DD",
						defaultValue : moment().format("YYYY-MM-DD"),
						readonly : true,
						validate : [ "required" ]
					} ]
				}
			});
			widget.set("applyForm", applyForm);
		}
	};
	module.exports = instance;
});
